package com.stn.zjx.givingtreasure.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.demo.ShopShow;
import com.stn.zjx.givingtreasure.widget.MusicBoView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.zhanghai.android.materialratingbar.MaterialRatingBar;

public class ShopItemListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public List<ShopShow> dataBeanList = new ArrayList<>();

    private LayoutInflater inflater;
    public BaseActivity context;

    public ShopItemListAdapter(BaseActivity context) {
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder vh = new IndexHolder(inflater.inflate(R.layout.item_shop_show, parent, false));
        return vh;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        IndexHolder ih = (IndexHolder) viewHolder;
        ShopShow data = dataBeanList.get(position);

        Glide.with(context)
                .load(data.getShopLogo())
                .apply(new RequestOptions().fitCenter())
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(ih.shopIv);
        ih.shopNameTv.setText(data.getShopName());
        ih.shopFollowCountTv.setText(String.valueOf(data.getFollowShopCount()));
        ih.shopRecommandStarTv.setText(String.valueOf(data.getRecommand()));


        ih.shopCommentStarTv.setRating((float) data.getCommentStart());
        ih.shopCommentCountTv.setText(String.valueOf(data.getCommentCount()));
        ih.shopAddressTv.setText("地址:" + data.getAddress());
        ih.shopAddressDistanceTv.setText("" + data.getAddressNear());
        if (!TextUtils.isEmpty(data.getShopVideos())) {
            ih.shopVideoIv.setImageResource(R.mipmap.blue_watch_video);
        } else {
            ih.shopVideoIv.setImageResource(R.mipmap.gray_watch_video);
        }
        switch (data.getIsLiving()) {
            case 0:
                ih.shopLivingTv.setTextColor(ActivityCompat.getColor(context, R.color.gray));
                ih.shopLivingTv.setText(context.getString(R.string.no_live));
                ih.musicView.setVisibility(View.GONE);
                break;
            case 1:
                ih.shopLivingTv.setTextColor(ActivityCompat.getColor(context, R.color.default_red1));
                ih.shopLivingTv.setText(context.getString(R.string.loading_live));
                ih.musicView.setVisibility(View.VISIBLE);

                break;
        }
        if (data.getShopDisplayImgs() != null && data.getShopDisplayImgs().size() != 0) {
            String   img= data.getShopDisplayImgs().get(0);
            Glide.with(context)
                    .load(img)
                    .apply(new RequestOptions().fitCenter())
                    .transition(DrawableTransitionOptions.withCrossFade())
                    .into(ih.shopImgIndexIv);
            ih.shopImgCountTv.setText("+"+data.getShopDisplayImgs().size());
            ih.shopImgRl.setVisibility(View.VISIBLE);

        } else {
            ih.shopImgCountTv.setText("+0");
           ih.shopImgRl.setVisibility(View.GONE);
        }
        switch (data.getShowType()){
            case 0:
                ih.shopTypeTv.setVisibility(View.GONE);
                break;
            case 1:
                ih.shopTypeTv.setVisibility(View.VISIBLE);
                ih.shopTypeTv.setText("生活");
                break;
            case 2:
                ih.shopTypeTv.setVisibility(View.VISIBLE);
                ih.shopTypeTv.setText("服装");
                break;
            case 3:
                ih.shopTypeTv.setVisibility(View.VISIBLE);
                ih.shopTypeTv.setText("美妆护理");
                break;
            case 4:
                ih.shopTypeTv.setVisibility(View.VISIBLE);
                ih.shopTypeTv.setText("健康");
                break;
            case 5:
                ih.shopTypeTv.setVisibility(View.VISIBLE);
                ih.shopTypeTv.setText("电器");
                break;
            case 6:
                ih.shopTypeTv.setVisibility(View.VISIBLE);
                ih.shopTypeTv.setText("数码");
                break;
            case 7:
                ih.shopTypeTv.setVisibility(View.VISIBLE);
                ih.shopTypeTv.setText("餐饮");
                break;
            case 8:
                ih.shopTypeTv.setVisibility(View.VISIBLE);
                ih.shopTypeTv.setText("杂物");
                break;

            case 9:
                ih.shopTypeTv.setVisibility(View.VISIBLE);
                ih.shopTypeTv.setText("网游");
                break;
            case 10:
                ih.shopTypeTv.setVisibility(View.VISIBLE);
                ih.shopTypeTv.setText("工艺");
                break;
            case 11:
                ih.shopTypeTv.setVisibility(View.VISIBLE);
                ih.shopTypeTv.setText("其他");
                break;

        }
    }

    @Override
    public int getItemCount() {
        return dataBeanList.size();
    }

    public void refreshList(List<ShopShow> dataBeans) {
        dataBeanList.removeAll(dataBeanList);
        dataBeanList.addAll(dataBeans);
        notifyDataSetChanged();
    }

    public void appendList(List<ShopShow> dataBeans) {
        dataBeanList.addAll(dataBeans);
        notifyDataSetChanged();
    }

    public void delete(int position) {
        dataBeanList.remove(position);
        notifyDataSetChanged();
    }

    public void clearList() {
        dataBeanList.removeAll(this.dataBeanList);
        notifyDataSetChanged();
    }


    public class IndexHolder extends RecyclerView.ViewHolder {

        public View rootView;
        @BindView(R.id.shop_iv)
        ImageView shopIv;
        @BindView(R.id.shop_name_tv)
        TextView shopNameTv;
        @BindView(R.id.shop_follow_count_tv)
        TextView shopFollowCountTv;
        @BindView(R.id.shop_recommand_star_tv)
        TextView shopRecommandStarTv;
        @BindView(R.id.shop_img_index_iv)
        ImageView shopImgIndexIv;
        @BindView(R.id.shop_img_count_tv)
        TextView shopImgCountTv;
        @BindView(R.id.shop_img_rl)
        RelativeLayout shopImgRl;
        @BindView(R.id.shop_comment_star_tv)
        MaterialRatingBar shopCommentStarTv;
        @BindView(R.id.shop_comment_count_tv)
        TextView shopCommentCountTv;
        @BindView(R.id.shop_address_tv)
        TextView shopAddressTv;
        @BindView(R.id.shop_video_iv)
        ImageView shopVideoIv;
        @BindView(R.id.shop_video_tv)
        RelativeLayout shopVideoTv;
        @BindView(R.id.shop_living_tv)
        TextView shopLivingTv;
        @BindView(R.id.music_view)
        MusicBoView musicView;
        @BindView(R.id.loading_live)
        LinearLayout loadingLive;
        @BindView(R.id.shop_address_distance_tv)
        TextView shopAddressDistanceTv;
        @BindView(R.id.shop_type_tv)
        TextView shopTypeTv;

        @SuppressLint("ClickableViewAccessibility")
        IndexHolder(@NonNull View itemView) {
            super(itemView);
            rootView = itemView;
            ButterKnife.bind(this, itemView);
            shopCommentStarTv.setClickable(false);
            shopCommentStarTv.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    return false;
                }
            });
        }
    }


}